package org.lionsteins.eddie.server;

import java.sql.Date;
import java.util.ArrayList;

import org.hibernate.Query;
import org.hibernate.Session;
import org.lionsteins.eddie.client.GreetingService;
import org.lionsteins.eddie.client.AlertBean;
import org.lionsteins.eddie.domain.Alert;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

  
  
	
	public ArrayList<AlertBean> getAlerts(Date sDate, Date eDate){
		 return getAlertRecords(sDate, eDate);
	}
	
	public static ArrayList<AlertBean> getAlertRecords (Date start, Date end){
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		
		session.beginTransaction();
		System.out.println("start date: "+start.toLocaleString());
		System.out.println("end date: "+end.toLocaleString());
		Query query = session.createQuery("from Alert where date >= :start and date <= :end");
		query.setParameter("start", start);
		query.setParameter("end", end);
		
		ArrayList<org.lionsteins.eddie.domain.Alert> temp = (ArrayList<Alert>) query.list();
		ArrayList<org.lionsteins.eddie.client.AlertBean> list = new ArrayList<org.lionsteins.eddie.client.AlertBean>();
		for (Alert alert: temp){
			System.out.println("added one record");
    		System.out.println("lat lon: "+ alert.getLat() + "  " + alert.getLon());
			list.add(new org.lionsteins.eddie.client.AlertBean(alert.getDate(), alert.getId(), alert.getIncidentType(), 
				alert.getLat(), alert.getLon(), alert.getLocation(), alert.getSender()));
		}
		System.out.println("list size: "+ temp.size());
		
		return list;
		
	}

  
}
